var NewValidation = function () {

    var handleValidation1 = function() {
        var form1 = $('#submit_form');
        var error1 = $('.alert-danger', form1);
        var success1 = $('.alert-success', form1);
        var administrator_id = $('#administrator_id').val();
        $.validator.addMethod("isOld",function(value){
            var username = value;
            $.ajax({
                type:"POST",
                async:false,
                url:"/Manage/index.php?m=Manage&c=User&a=ajaxCheckUserPass",
                data:{param:username},
                success:function(response){
                    if(response != 0){
                        res = false;
                    }else{
                        res = true;
                    }
                }
            });
            return res;
        });
        form1.validate({
            focusCleanup: true, //focus对应对象时隐藏错误提示
            focusInvalid: false,//验证后让未通过验证的第一个表单元素获得焦点
            errorClass: "help-block",
            validClass: "valid-ok",
            errorElement: "span",
            submitHandler: function(form){ //当表单验证成功并提交时执行

                $(form).ajaxSubmit({

                    success: function(res) {

                        bootbox.dialog({
                            message: res.info,
                            title: "",
                            buttons: {
                                success: {
                                    label: "OK",
                                    className: (res.status==1)?"green":"red",
                                    callback: function() {
                                        window.location.href = htmlspecialchars_decode(res.url);
                                    }
                                }
                            }
                        });

                    },

                    error: function (XmlHttpRequest, textStatus, errorThrown) {
                        console.log(errorThrown);
                    },
                    url:form1.attr('action'), /*设置post提交到的页面*/
                    type: 'post', /*设置表单以post方法提交*/
                    resetForm: false
                });
            },
            rules: { 
                oldpwd:{
                    required:true,
                    isOld:true,
                }, 
                newpwd:{
                    required:true,
                }, 
                chkpwd:{
                    required:true,
                    equalTo: "#newpwd"

                },

            },
            messages: {
                oldpwd:{
                    required:"请输入旧密码",
                    isOld:"旧密码错误"
                },
                newpwd:{
                    required:"请输入新密码",
                }, 
                chkpwd:{
                    required:"请输入确认密码",
                    equalTo: "密码不一致，请重新输入",

                },
            },
            invalidHandler: function (event, validator) { //display error alert on form submit
                success1.hide();
                error1.show();
                App.scrollTo(error1, -200);
            },

            highlight: function (element) { // hightlight error inputs
                $(element)
                    .closest('.form-group').addClass('has-error'); // set error class to the control group
            },

            unhighlight: function (element) { // revert the change done by hightlight
                $(element)
                    .closest('.form-group').removeClass('has-error'); // set error class to the control group
            },

            success: function (label) {
                label
                    .closest('.form-group').removeClass('has-error'); // set success class to the control group
            }

        });
    }
    return {
        //main function to initiate the module

        init: function () {

            handleValidation1();

        }
    };

}();